package wviz.gui.graph;

import prefuse.controls.ControlAdapter;
import prefuse.util.FontLib;
import prefuse.util.ui.JFastLabel;
import prefuse.util.ui.UILib;
import prefuse.visual.VisualItem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;

/**
 * A JPanel subclass which will be used for attaching to main window.
 * Some components of the this panel will be used in different places in the main window, so this class acts as a place
 * holder for accessing these components
 * <p/>
 */

public class GraphPanel extends JPanel {
    private final ValueSlider filterLevelSlider;
    private BaseDisplay display;
    /**
     * Create a container panel, search box and level filtering slider for passed display parameter as well as adding
     * required listeners to display object.
     * <p/>
     * Currently two display types are supported: Radial and Spider
     * <p/>
     *
     * @param baseDisplay    the prefuse display object - graph will be rendered in this object
     * @param title          the initial title in left upper corner of panel
     * @param searchField    the data field (Table column) to query
     * @param filterLabel    the label used for level filtering slider
     * @param filterMaxValue maximum value of level filtering slider
     */
    public GraphPanel(final BaseDisplay baseDisplay, String title, final String searchField,
                      String filterLabel, final int filterMaxValue) {
        super(new BorderLayout());
        this.display = baseDisplay;


      //  final JFastLabel titleLabel = new JFastLabel(title);
//        titleLabel.setPreferredSize(new Dimension(170, 30));
//        titleLabel.setVerticalAlignment(SwingConstants.BOTTOM);
//        titleLabel.setBorder(BorderFactory.createEmptyBorder(3, 0, 1, 0));
//        titleLabel.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 14));
//
//        add(titleLabel, BorderLayout.PAGE_START);
        add(display, BorderLayout.CENTER);

        Color BACKGROUND = Color.WHITE;
        Color FOREGROUND = Color.DARK_GRAY;
        UILib.setColor(this, BACKGROUND, FOREGROUND);

        filterLevelSlider = new ValueSlider(filterLabel, 0, filterMaxValue,
                (int) Math.floor((double) filterMaxValue / 2), false); //changed by Mamad
//        add change listener
        
//        SearchTupleSet tupleSet = (SearchTupleSet) vis.getGroup(Visualization.SEARCH_ITEMS);
//        tupleSet.addTupleSetListener(new TupleSetListener() {
//            public void tupleSetChanged(TupleSet tset, Tuple[] added, Tuple[] removed) {
//                if (removed.length > 0) {
//                    //notify applet to remove hilight from text
//                    motherApplet.removeHilighting(searchBox.getQuery());
//                }
//                if (added.length > 0) {
//                    //notify applet to add hilighting to text
//                    motherApplet.addHilighting(searchBox.getQuery());
//                }
//            }
//        });
//
//        display.addControlListener(new ControlAdapter() {
//            public void itemEntered(VisualItem item, MouseEvent e) {
//                if (item.canGetString(searchField))
//                    titleLabel.setText(item.getString(searchField));
//            }
//
//            public void itemExited(VisualItem item, MouseEvent e) {
//                titleLabel.setText(null);
//            }
//        });

        display.setFilterLevel(filterLevelSlider.getValue().intValue());

        add(display);
    }


    public ValueSlider getFilterLevelSlider() {
        return filterLevelSlider;
    }

    /**
     * Remove previous display (if added), add new display to this panel, update display filter level and repaint this panel.
     * fields changing when toggling between displays :
     *  display - spider or radial
     *  dataGroup - SpiderDisplay.NODES or RadialDisplay.TREE_NODES
     *  filterLabel - Search or Filter
     *  filterMaxValue - 100 or 10
     * @param toBeUpdate the new display graph
     */
    public void setupDisplay(BaseDisplay toBeUpdate) {
    	
        this.remove(this.display);      
        this.display = toBeUpdate;
        this.add(this.display, BorderLayout.CENTER);
    
        filterLevelSlider.update(this.display);
        this.display.setFilterLevel(filterLevelSlider.getValue().intValue());
        filterLevelSlider.paintAll(filterLevelSlider.getGraphics());
        this.paintAll(this.getGraphics());
    }
}